package com.ontheroad.core.util;

/**
 * 坐标、像素转换工具类
 * <p>
 * Created by kedong on 2017/7/29 0029.
 */
public class CoordinatePixel {
    // X轴上每像素代表的经度秒数
    private double scaleX = 0d;
    // Y轴上每像素代表的纬度秒数
    private double scaleY = 0d;

    private double maxLon = 0d;
    private double minLon = 0d;
    private double maxLat = 0d;
    private double minLat = 0d;
    // 宽
    private int h = 400;
    // 高
    private int y = 300;

    /**
     * 初始化转换因子，即每像素所代表的经度和纬度
     *
     * @param maxLon 最大经度
     * @param minLon 最小经度
     * @param maxLat 最大纬度
     * @param minLat 最小纬度
     */
    public CoordinatePixel(double maxLon, double minLon, double maxLat, double minLat) {
        scaleX = ((maxLon - minLon) * 3600) / h;
        scaleY = ((maxLat - minLat) * 3600) / y;
        this.maxLat = maxLat;
        this.maxLon = maxLon;
        this.minLat = minLat;
        this.minLon = minLon;
    }

    /**
     * 经纬度转屏幕坐标
     *
     * @param lon
     * @param lat
     * @return
     */
    public double[] coordinate_conversion_pixel(double lon, double lat) {
        double X = (lon - minLon) * 3600 / scaleX;
        double Y = (maxLat - lat) * 3600 / scaleY;
        double[] pixel = {X, Y};
        return pixel;
    }

    /**
     * 屏幕坐标转经纬度坐标
     *
     * @param X
     * @param Y
     * @return
     */
    public double[] pixel_conversion_coordinate(double X, double Y) {
        double lon = X * scaleX / 3600 + minLon;
        double lat = maxLat - Y * scaleY / 3600;
        double[] coordinate = {lon, lat};
        return coordinate;
    }
}
